/**
 * JAVACC DEMO 1.0
 * @copy right zbxsoft company All rights reserved. 
 * @Package com.zbxsoft.platform.template.helper  
 */
package com.apache.design.template.helper;

import com.apache.design.template.FreemarkerHelper;
import com.apache.design.util.DesignEntity;
import com.apache.design.util.FileOperate;
import com.apache.design.util.GZipUtil;
import com.apache.info.entity.PubSortInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  生成其他实现类
 */
public class DaoTempManager extends SupperTemplateManager {

	public boolean doInvoke(DesignEntity entity) {
		boolean mark = false;
		List<PubSortInfo> list = entity.getTabs();
		String jarPath = entity.getPackPath();
		String prefix = entity.getProjectName() + "/src/main/";
		String xmlPath = entity.getFilePath() + "/" + prefix + "java/" + jarPath.replace(".", "/");
		String sysName = entity.getSysEname();

		try {
			createManager(list, jarPath, prefix, xmlPath, sysName);
			mark = true;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("生成其他文件失败", e);
		}
		return mark;
	}

	private void createManager(List<PubSortInfo> list, String jarPath, String projectName, String filePath,
			String sysName) throws Exception {
		FileOperate.getInstance().newCreateFolder(filePath + "/dao/");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("packPath", jarPath);
		data.put("reqName", sysName);
		data.put("sysName", GZipUtil.toUpperFirst(sysName));

		for (PubSortInfo map : list) {
			String objName = GZipUtil.toUpperFirst(map.getObjName());
			data.put("sort", map);
			String result = FreemarkerHelper.instance().getTemplateStr("dao/ObjNameDaoImpl.inc", data);
			FileOperate.getInstance().newCreateFile(filePath + "/dao/" + objName + "DaoImpl.java", result);
		}
	}

}
